package com.btex.app;

import android.app.Dialog;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.IdRes;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.btex.app.adapter.TradeDownAdapter;
import com.btex.app.adapter.TradeUpAdapter;
import com.btex.app.adapter.TradesAdapter;
import com.btex.app.entity.OrderBookInfo;
import com.btex.app.entity.TexchangeRateInfo;
import com.btex.app.entity.TradeInfo;
import com.btex.app.entity.UserMoneyInfo;
import com.btex.app.tools.CookieTool;
import com.btex.app.tools.MicroAVLIDialog;
import com.btex.app.tools.SharedPreferencesUtil;
import com.btex.app.tools.Tools;
import com.btex.app.tools.Util;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.util.PreferencesCookieStore;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.json.JSONException;
import org.json.JSONObject;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.btex.app.R.string.num;


public class TradeActivityss extends BaseActivity implements RadioGroup.OnCheckedChangeListener {

    @BindView(R.id.iv_back)
    ImageView mIvBack;
    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.tv_menu)
    TextView mTvMenu;
    @BindView(R.id.rl_title)
    RelativeLayout mRlTitle;
    @BindView(R.id.ivCoinImg)
    ImageView mIvCoinImg;
    @BindView(R.id.tvCoinName)
    TextView mTvCoinName;
    @BindView(R.id.tvCoinPrice)
    TextView mTvCoinPrice;
    @BindView(R.id.tvZhangDieFu)
    TextView mTvZhangDieFu;
    @BindView(R.id.ll_feed)
    LinearLayout mLlFeed;
    @BindView(R.id.tv_buy1_price)
    TextView mTvBuy1Price;
    @BindView(R.id.tv_seel1_price)
    TextView mTvSeel1Price;
    @BindView(R.id.tv_tradeNum)
    TextView mTvTradeNu;
    @BindView(R.id.tv_maxPrice)
    TextView mTvMaxPric;
    @BindView(R.id.tv_minPrice)
    TextView mTvMinPrice;
    @BindView(R.id.tv_tradePrice)
    TextView mTvTradePrice;
    @BindView(R.id.tab1)
    RadioButton mTab1;
    @BindView(R.id.tab2)
    RadioButton mTab2;
    @BindView(R.id.tab3)
    RadioButton mTab3;
    @BindView(R.id.switch_button)
    RadioGroup mSwitchButton;
    @BindView(R.id.activity_b_tv_name)
    TextView mActivityBTvName;
    @BindView(R.id.activity_b_k_imag)
    LinearLayout mActivityBKImag;
    @BindView(R.id.tvMaxPrice)
    TextView mTvMaxPrice;
    @BindView(R.id.ll_trade_history)
    LinearLayout mLlTradeHistory;
    @BindView(R.id.tvTradeNum)
    TextView mTvTradeNum;
    @BindView(R.id.ll_history_entrust)
    LinearLayout mLlHistoryEntrust;
    @BindView(R.id.ll_history)
    LinearLayout mLlHistory;
    @BindView(R.id.tv_avilableCoinName)
    TextView mTvAvilableCoinName;
    @BindView(R.id.tv_avilableRMB)
    TextView mTvAvilableRMB;
    @BindView(R.id.tv_avilableCoin)
    TextView mTvAvilableCoin;
    @BindView(R.id.ll_availabel)
    LinearLayout mLlAvailabel;
    @BindView(R.id.tv_price)
    TextView mTvPrice;
    @BindView(R.id.et_price)
    EditText mEtPrice;
    @BindView(R.id.tv_num)
    TextView mTvNum;
    @BindView(R.id.et_num)
    EditText mEtNum;
    @BindView(R.id.tv_total_price)
    TextView mTvTotalPrice;
    @BindView(R.id.tv_fei)
    TextView mTvFei;
    @BindView(R.id.et_tradePsw)
    EditText mEtTradePsw;
    @BindView(R.id.activity_b_but_dz)
    Button mActivityBButDz;
    @BindView(R.id.activity_header_trade_up)
    RecyclerView mActivityHeaderTradeUp;
    @BindView(R.id.activity_header_trade_down)
    RecyclerView mActivityHeaderTradeDown;
    @BindView(R.id.ll_shidang)
    LinearLayout mLlShidang;
    @BindView(R.id.ll_buy_sell)
    LinearLayout mLlBuySell;
    @BindView(R.id.activity_trade_kine_imag)
    ImageView mActivityTradeKineImag;
    @BindView(R.id.explain_type)
    TextView mExplainType;
    @BindView(R.id.explain_typ_value)
    TextView mExplainTypValue;
    @BindView(R.id.activity_trade_recy)
    RecyclerView mActivityTradeRecy;
    @BindView(R.id.bg1)
    View mBg1;
    @BindView(R.id.bg2)
    View mBg2;
    @BindView(R.id.bg3)
    View mBg3;
    @BindView(R.id.btnConfirm)
    Button mBtnConfirm;
    @BindView(R.id.activity_trade_jian)
    ImageView mActivityTradeJian;
    @BindView(R.id.activity_trade_jia)
    ImageView mActivityTradeJia;
    @BindView(R.id.activity_trade_k_info)
    LinearLayout mActivityTradeKInfo;
    @BindView(R.id.activity_trade_psw)
    LinearLayout mActivityTradePsw;
    @BindView(R.id.activity_reabution_one)
    RadioButton mActivityReabutionOne;
    @BindView(R.id.activity_reabution_two)
    RadioButton mActivityReabutionTwo;
    @BindView(R.id.activity_reabution_three)
    RadioButton mActivityReabutionThree;
    @BindView(R.id.activity_reabution_four)
    RadioButton mActivityReabutionFour;
    @BindView(R.id.activity_reabution_fine)
    RadioButton activity_reabution_fine;
    @BindView(R.id.activity_trade_tv_hui_lv)
    TextView mActivityTradeTvHuiLv;
    @BindView(R.id.activity_trade_tv_hui_cny)
    TextView mActivityTradeTvHuiCny;
    @BindView(R.id.activity_trade_tv_danwei)
    TextView mActivityTradeTvDanwei;
    @BindView(R.id.activity_trade_tv_about)
    TextView mActivityTradeTvAbout;

    @BindView(R.id.activity_trade_total_money)
    TextView mActivityTradeTotalMoney;
    @BindView(R.id.avtivity_trade_look_more)
    TextView mAvtivityTradeLookMore;
    @BindView(R.id.activity_rad_bi_li)
    RadioGroup mActivityRadBiLi;
    @BindView(R.id.explain_typ_money)
    TextView mExplainTypMoney;
    @BindView(R.id.activity_webview)
    WebView mActivityWebview;
    @BindView(R.id.tradeactivity_tv_die)
    TextView mTradeactivityTvDie;
    @BindView(R.id.tradeactivity_tv_zhang)
    TextView mTradeactivityTvZhang;
    @BindView(R.id.tradeactivity_rela_zhangdie)
    RelativeLayout mTradeactivityRelaZhangdie;
    @BindView(R.id.trade_progess_zhang_die)
    ProgressBar mTradeProgessZhangDie;
    @BindView(R.id.activity_header_trade_tv_price)
    TextView mActivityHeaderTradeTvPrice;
    @BindView(R.id.activity_header_trade_tv_number)
    TextView mActivityHeaderTradeTvNumber;
    @BindView(R.id.activity_header_trade_tv_cheng_jiao)
    TextView mActivityHeaderTradeTvChengJiao;
    @BindView(R.id.activity_header_trade_tv_cheng_jiao_time)
    TextView mActivityHeaderTradeTvChengJiaoTime;
    @BindView(R.id.activity_header_trade_tv_cheng_num)
    TextView mActivityHeaderTradeTvChengNum;
    @BindView(R.id.activity_header_trade_tv_cheng_price)
    TextView mActivityHeaderTradeTvChengPrice;

    @BindView(R.id.tv_zhangdie)
    TextView tv_zhangdie;

    private SharedPreferencesUtil sp;
    private String mToken;
    private boolean mIs_login;
    private boolean mIs_mai = true;
    Double d = 0.00000;
    private boolean isBuy = true;
    private PreferencesCookieStore cookieStore;
    private String mType = "";
    private String mDanwei = "";
    private UserMoneyInfo mUserMoneyInfo;
    private Double mDoublez = 0.0;
    private Double mDoublez1 = 0.0;

    public boolean isShow;
    private String mS1 = "0.00";
    private MicroAVLIDialog mMicroAVLIDialog;
    private String mS = "0.00";
    private String mPrice_float_num = "1";
    private Double mMEtNum = 0.0;
    public String typePair;
    public String urls;
    private boolean mIsFirst;
    private Map<String, String> mExtraHeaders;
    private String mFloat_num;
    private String mCity_new;
    private Resources mResources;
    private Configuration mConfiguration;
    private DisplayMetrics mDisplayMetrics;

    private int beforeDot = 8;
    private int afterDot = 8;

    public double jiajian = 0.0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_list_refresh);
//        View headView = View.inflate(cxt, R.layout.activity_header_trade, null);
        setContentView(R.layout.activity_header_trade);
        ButterKnife.bind(this);
        sp = SharedPreferencesUtil.getSharedPreferences(this);
        mCity_new = sp.getString("city_new", "0");
        mResources = getResources();
        mConfiguration = mResources.getConfiguration();
        mDisplayMetrics = mResources.getDisplayMetrics();

        setCity();
        initTV();
        mActivityTradeRecy.setNestedScrollingEnabled(false);
        mMicroAVLIDialog = new MicroAVLIDialog(this);
        isShow = true;
        initEditListener();
        mMicroAVLIDialog.show();

        mToken = sp.getString(Config.TOKEN, "");

        String token = sp.getString(Config.TOKEN, null);
        boolean isEngilsh = sp.getBoolean("isEngilsh", false);
        initButton();
        initRecyclerview();
        mSwitchButton.setOnCheckedChangeListener(this);
        mActivityRadBiLi.setOnCheckedChangeListener(this);
        String pair = (String) getIntent().getExtras().get("pair");

        mActivityWebview.setVisibility(View.GONE);
        mActivityWebview.setWebChromeClient(new WebChromeClient());
        mActivityWebview.setWebViewClient(new WebViewClient());
        mActivityWebview.getSettings().setJavaScriptEnabled(true);


//        预加载webview
        if (!TextUtils.isEmpty(pair)) {
            if (isEngilsh) {

                mExtraHeaders = new HashMap<String, String>();
                mExtraHeaders.put("Cookie", token);
                mExtraHeaders.put("Token", token);
                mExtraHeaders.put("SESSID", token);
                mExtraHeaders.put("myversion", Tools.getVersion(this));
                mExtraHeaders.put("myclient", "android");
                mExtraHeaders.put("lang", "en");
                urls = Config.URL + "home/orders/" + pair + "?lang=en";
            } else {
                mExtraHeaders = new HashMap<String, String>();
                mExtraHeaders.put("Cookie", token);
                mExtraHeaders.put("Token", token);
                mExtraHeaders.put("SESSID", token);
                mExtraHeaders.put("myversion", Tools.getVersion(this));
                mExtraHeaders.put("myclient", "android");
                mExtraHeaders.put("lang", "zh_cn");
                urls = Config.URL + "home/orders/" + pair + "?lang=zh_cn";
            }

            mActivityWebview.loadUrl(urls, mExtraHeaders);

            typePair = pair;
            String[] split = typePair.split("_");
            mType = split[0];
            mDanwei = split[1];

            mActivityBTvName.setText(mType + "/" + mDanwei);
            mActivityTradeTvDanwei.setText(mType);
            mActivityTradeTotalMoney.setText("0.00" + "\t" + mDanwei);


//            得到汇率相关
            geTexchangeRate();
//            获取五档数据
            getOrderbook();
//            成交历史
            getTradesInfo();
//            获取用户余额
            getUserMoneyInfo(mType, mDanwei);
            mTvTitle.setText(mType + "/" + mDanwei + getString(R.string.trade));

            Log.i("wekfhwehfuweifw", typePair);
        }


        mIs_login = sp.getBoolean("is_login", false);

        if (mIs_login) {
            if (mIsFirst) {

                mActivityTradePsw.setVisibility(View.GONE);
            } else {
                mActivityTradePsw.setVisibility(View.VISIBLE);
            }
            mBtnConfirm.setVisibility(View.VISIBLE);
            mActivityBButDz.setVisibility(View.GONE);

            mBtnConfirm.setText(getString(R.string.corn_buy) + " " + mType);

        } else {

            mActivityTradePsw.setVisibility(View.GONE);
            mBtnConfirm.setVisibility(View.GONE);
            mActivityBButDz.setVisibility(View.VISIBLE);
        }

        mExplainType.setText(getString(R.string.available_rmb));

    }

    private void initTV() {
        mActivityHeaderTradeTvPrice.setText(getString(R.string.input_buy_price));
        mActivityHeaderTradeTvNumber.setText(getString(num));
        mActivityHeaderTradeTvChengJiao.setText(getString(R.string.new_cheng_jiao));
        mActivityHeaderTradeTvChengJiaoTime.setText(getString(R.string.trade_time));
        mActivityHeaderTradeTvChengNum.setText(getString(R.string.shu_e));
        mActivityHeaderTradeTvChengPrice.setText(getString(R.string.trade_price_history));
        mTvTotalPrice.setText(getString(R.string.total_price));
        mAvtivityTradeLookMore.setText(getString(R.string.look_more));

    }

    private void initEditListener() {
        mEtPrice.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {


            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {


            }

            @Override
            public void afterTextChanged(Editable editable) {

                String s = editable.toString().toString();
//                if (s.length() > 8) {
//                    editTextable(mEtPrice, false);
//                } else {
//                    editTextable(mEtPrice, true);
//                }

                if (!TextUtils.isEmpty(s)) {

                    if (s.contains(",")) {
                        String replace = s.replace(",", "");
                        s = replace;
                    }

                    if (s.contains(".")) {

                        String[] split = s.split("\\.");
                        int length = split.length;
                        if (length >= 2) {
                            String s1 = split[1];
                            afterDot = Integer.valueOf(mPrice_float_num);
//                            if (s1.length() > integer - 1) {
////                                Toast.makeText(cxt, "最多输入" + integer + "小数点", Toast.LENGTH_SHORT).show();
//                                editTextable(mEtPrice, false);
//                            } else {
//                                editTextable(mEtPrice, true);
//                            }

                            judge(editable);

                        }


                    }

                    Double aDouble = Double.valueOf(s);

                    double mEtPrice = mMEtNum * aDouble;

                    String EtPrice = double2Str(mEtPrice);
                    mActivityTradeTotalMoney.setText(EtPrice + "\t" + mDanwei);


                    d = aDouble;
                    boolean empty = TextUtils.isEmpty(s);
                    if (!empty) {
                        mDoublez1 = doublez(s);
                        double v = mDoublez * mDoublez1;
                        String s1 = doubleStr(v);
                        boolean isEnglish = sp.getBoolean("isEngilsh", false);
                        if (isEnglish) {
                            mActivityTradeTvAbout.setText("≈" + "$" + s1);
                        } else {
                            mActivityTradeTvAbout.setText("≈" + "￥" + s1);
                        }
                    } else {
                        boolean isEnglish = sp.getBoolean("isEngilsh", false);
                        if (isEnglish) {
                            mActivityTradeTvAbout.setText("≈" + "$" + "0.00");
                            mActivityTradeTotalMoney.setText("0.00" + "\t" + mDanwei);
                        } else {
                            mActivityTradeTvAbout.setText("≈" + "￥" + "0.00");
                            mActivityTradeTotalMoney.setText("0.00" + "\t" + mDanwei);
                        }
                    }
                } else {
                    boolean isEnglish = sp.getBoolean("isEngilsh", false);
                    if (isEnglish) {
                        mActivityTradeTvAbout.setText("≈" + "$" + "0.00");
                        mActivityTradeTotalMoney.setText("0.00" + "\t" + mDanwei);
                    } else {
                        mActivityTradeTvAbout.setText("≈" + "￥" + "0.00");
                        mActivityTradeTotalMoney.setText("0.00" + "\t" + mDanwei);
                    }
                }


            }
        });


        mEtNum.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {

                String trim = editable.toString().trim();
                boolean empty = TextUtils.isEmpty(trim);
                if (!empty) {

                    if (trim.contains(",")) {
                        String replace = trim.replace(",", "");
                        trim = replace;
                    }

                    if (trim.contains(".")) {

                        String[] split = trim.split("\\.");
                        int length = split.length;
                        if (length >= 2) {
                            String s0 = split[0];
                            String s1 = split[1];
                            afterDot = Integer.valueOf(mFloat_num);
//                            if (s1.length() > integer - 1) {
//                                editTextable(mEtNum, false);
////                                hideEd(mEtNum);
////                                SnackbarShow("最多输入" + integer + "小数点");
////                                Toast.makeText(cxt, "最多输入" + integer + "小数点", Toast.LENGTH_SHORT).show();
//                            } else {
//                                editTextable(mEtNum, true);
//                            }

                            judge(editable);
                        }
                    }

                    mMEtNum = doublez(trim);
                    double v = mDoublez1 * mMEtNum;
                    String s = double2Str(v);
                    mActivityTradeTotalMoney.setText(s + "\t" + mDanwei);

                } else {
                    initTextView();
                }

            }
        });


    }

    private void geTexchangeRate() {
        int num = (int) ((Math.random() * 9 + 1) * 100000);
        String lang = "";
        final boolean isEngilsh = sp.getBoolean("isEngilsh", false);
        if (isEngilsh) {
            lang = "en";

        } else {
            lang = "zh_cn";
        }

        OkGo.<String>get(Config.URL_PAIR + "?rnd=" + String.valueOf(num)).params(Config.KEY_PAIR, typePair).params("lang", lang).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                String result = response.body().toString();

                try {
                    JSONObject jsonObject = new JSONObject(result);
                    if ("1".equals(jsonObject.getString(Config.KEY_CODE))) {

                        Gson gson = new Gson();
                        TexchangeRateInfo texchangeRateInfo = gson.fromJson(result, TexchangeRateInfo.class);

                        setShowZhangDie(texchangeRateInfo);
                        String rate_price = texchangeRateInfo.getData().getTrade_price();
                        mPrice_float_num = texchangeRateInfo.getData().getPrice_float_num();
                        setJiaJian(mPrice_float_num);
                        mFloat_num = texchangeRateInfo.getData().getAmount_float_num();
                        String change_24h = texchangeRateInfo.getData().getChange_24h();
                        Double change_24hDouble = Double.valueOf(change_24h);

                        if (change_24hDouble < 0) {
                            tv_zhangdie.setTextColor(getResources().getColor(R.color.red));
                            double v = change_24hDouble * 100;
                            String s3 = doubleStr(v);
                            tv_zhangdie.setText(s3 + "%");

                        } else {
                            tv_zhangdie.setTextColor(getResources().getColor(R.color.bg_green));
                            double v = change_24hDouble * 100;
                            String s3 = doubleStr(v);
                            tv_zhangdie.setText("+" + s3 + "%");
                        }


                        String rate_price1 = texchangeRateInfo.getData().getRate_price();

                        mDoublez = doublez(rate_price1);

                        if (!TextUtils.isEmpty(mPrice_float_num)) {

                            if (rate_price.contains(".")) {
                                String[] split = rate_price.split("\\.");
                                String s = split[1];
                                String s1 = split[0];
                                if (s.length() > Integer.parseInt(mPrice_float_num)) {
                                    String substring = s.substring(0, Integer.parseInt(mPrice_float_num));
                                    String s2 = s1 + "." + substring;
                                    mActivityTradeTvHuiLv.setText(s2);
                                } else {
                                    mActivityTradeTvHuiLv.setText(rate_price);
                                }
                            } else {
                                mActivityTradeTvHuiLv.setText(rate_price);
                            }
                        }

                        Double Trade_price = Double.valueOf(rate_price);
                        Double rate = Double.valueOf(rate_price1);
                        double mul = mul(Trade_price, rate);
                        Log.i("eiurhgiuehgrue", Trade_price + "==========" + rate_price1);
                        String s = double2Str(mul);

                        boolean isEngilsh1 = sp.getBoolean("isEngilsh", false);
                        if (isEngilsh1) {
                            if (s.contains(",")) {
                                String replace = s.replace(",", "");

                                if (replace.contains(".")) {
                                    String[] split = rate_price.split("\\.");
                                    String ss = split[1];
                                    String s1 = split[0];
                                    if (s.length() > Integer.parseInt(mPrice_float_num)) {
                                        String substring = ss.substring(0, Integer.parseInt(mPrice_float_num));
                                        String s2 = s1 + "." + substring;

                                        Double doublez = doublez(s2);
                                        if (doublez < 0.0001) {
                                            mActivityTradeTvHuiCny.setText("<" + "$" + "0.0001");
                                        } else {
                                            mActivityTradeTvHuiCny.setText("≈" + "$" + s2);
                                        }

                                    } else {
                                        Double doublez = doublez(replace);
                                        if (doublez < 0.0001) {
                                            mActivityTradeTvHuiCny.setText("<" + "$" + "0.0001");
                                        } else {
                                            mActivityTradeTvHuiCny.setText("≈" + "$" + replace);
                                        }

                                    }
                                } else {
                                    Double doublez = doublez(replace);
                                    if (doublez < 0.0001) {
                                        mActivityTradeTvHuiCny.setText("<" + "$" + "0.0001");
                                    } else {
                                        mActivityTradeTvHuiCny.setText("≈" + "$" + replace);
                                    }

                                }

                            } else {

                                if (s.contains(".")) {
                                    String[] split = s.split("\\.");
                                    String ss = split[1];
                                    String s1 = split[0];
                                    if (ss.length() > Integer.parseInt(mPrice_float_num)) {
                                        String substring = ss.substring(0, Integer.parseInt(mPrice_float_num));
                                        String s2 = s1 + "." + substring;
                                        Double doublez = doublez(s2);
                                        if (doublez < 0.0001) {
                                            mActivityTradeTvHuiCny.setText("<" + "$" + "0.0001");
                                        } else {
                                            mActivityTradeTvHuiCny.setText("≈" + "$" + s2);
                                        }

                                    } else {
                                        Double doublez = doublez(s);
                                        if (doublez < 0.0001) {
                                            mActivityTradeTvHuiCny.setText("<" + "$" + "0.0001");
                                        } else {
                                            mActivityTradeTvHuiCny.setText("≈" + "$" + s);
                                        }

                                    }
                                } else {
                                    Double doublez = doublez(s);
                                    if (doublez < 0.0001) {
                                        mActivityTradeTvHuiCny.setText("<" + "$" + "0.0001");
                                    } else {
                                        mActivityTradeTvHuiCny.setText("≈" + "$" + s);
                                    }

                                }
                            }

                        } else {

                            if (s.contains(",")) {
                                String replace = s.replace(",", "");
                                Log.i("kwehfkwuheifuhwiehfiw", replace);
                                if (replace.contains(".")) {
                                    String[] split = replace.split("\\.");
                                    String ss = split[1];
                                    String s1 = split[0];
                                    if (s.length() > Integer.parseInt(mPrice_float_num)) {
                                        String substring = ss.substring(0, Integer.parseInt(mPrice_float_num));
                                        String s2 = s1 + "." + substring;

                                        Double doublez = doublez(s2);
                                        if (doublez < 0.0001) {
                                            mActivityTradeTvHuiCny.setText("<" + "￥" + "0.0001");
                                        } else {
                                            mActivityTradeTvHuiCny.setText("≈" + "￥" + s2);
                                        }

                                    } else {
                                        Double doublez = doublez(replace);
                                        if (doublez < 0.0001) {
                                            mActivityTradeTvHuiCny.setText("<" + "￥" + "0.0001");
                                        } else {
                                            mActivityTradeTvHuiCny.setText("≈" + "￥" + replace);
                                        }

                                    }
                                } else {

                                    Double doublez = doublez(replace);
                                    if (doublez < 0.0001) {
                                        mActivityTradeTvHuiCny.setText("<" + "￥" + "0.0001");
                                    } else {
                                        mActivityTradeTvHuiCny.setText("≈" + "￥" + replace);
                                    }

                                }

//                                mActivityTradeTvHuiCny.setText("≈" + "￥" + replace.substring(0, Integer.parseInt(mPrice_float_num)));
                            } else {
                                if (s.contains(".")) {
                                    String[] split = s.split("\\.");
                                    String ss = split[1];
                                    String s1 = split[0];
                                    if (s.length() > Integer.parseInt(mPrice_float_num)) {
                                        String substring = ss.substring(0, Integer.parseInt(mPrice_float_num));
                                        String s2 = s1 + "." + substring;
                                        Log.i("lwjfowieofjwie", s2);
                                        Double doublez = doublez(s2);
                                        if (doublez < 0.0001) {
                                            mActivityTradeTvHuiCny.setText("<" + "￥" + "0.0001");
                                        } else {
                                            mActivityTradeTvHuiCny.setText("≈" + "￥" + s2);
                                        }

                                    } else {
                                        Double doublez = doublez(s);
                                        if (doublez < 0.0001) {
                                            mActivityTradeTvHuiCny.setText("<" + "￥" + "0.0001");
                                        } else {
                                            mActivityTradeTvHuiCny.setText("≈" + "￥" + s);
                                        }

                                    }
                                } else {
                                    Double doublez = doublez(s);


                                    if (doublez < 0.0001) {
                                        mActivityTradeTvHuiCny.setText("<" + "￥" + "0.0001");
                                    } else {
                                        mActivityTradeTvHuiCny.setText("≈" + "￥" + s);
                                    }

                                }

//                                mActivityTradeTvHuiCny.setText("≈" + "￥" + replace.substring(0, Integer.parseInt(mPrice_float_num)));
                            }
                        }

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });


//        String lang = "";
//        final boolean isEngilsh = sp.getBoolean("isEngilsh", false);
//        if (isEngilsh) {
//            lang = "en";
//
//        } else {
//            lang = "zh_cn";
//        }
//        RequestParams params = new RequestParams();
//        params.addQueryStringParameter(Config.KEY_PAIR, typePair);
//        params.addQueryStringParameter("lang", lang);
//
//        new HttpUtils().send(HttpRequest.HttpMethod.GET, Config.URL_PAIR + "?rnd=" + String.valueOf(num), params, new RequestCallBack<String>() {
//            @Override
//            public void onSuccess(ResponseInfo<String> responseInfo) {
//
//                String result = responseInfo.result;
//                Log.i("kehbreuhriguheiugv", result);
//
//                try {
//                    JSONObject jsonObject = new JSONObject(result);
//                    if ("1".equals(jsonObject.getString(Config.KEY_CODE))) {
//
//                        Gson gson = new Gson();
//                        TexchangeRateInfo texchangeRateInfo = gson.fromJson(result, TexchangeRateInfo.class);
//
//                        setShowZhangDie(texchangeRateInfo);
//                        String rate_price = texchangeRateInfo.getData().getTrade_price();
//                        mPrice_float_num = texchangeRateInfo.getData().getPrice_float_num();
//                        mFloat_num = texchangeRateInfo.getData().getAmount_float_num();
//
//
//                        String rate_price1 = texchangeRateInfo.getData().getRate_price();
//
//                        mDoublez = doublez(rate_price1);
//
//                        if (!TextUtils.isEmpty(mPrice_float_num)) {
//
//                            if (rate_price.contains(".")) {
//                                String[] split = rate_price.split("\\.");
//                                String s = split[1];
//                                String s1 = split[0];
//                                if (s.length() > Integer.parseInt(mPrice_float_num)) {
//                                    String substring = s.substring(0, Integer.parseInt(mPrice_float_num));
//                                    String s2 = s1 + "." + substring;
//                                    mActivityTradeTvHuiLv.setText(s2);
//                                } else {
//                                    mActivityTradeTvHuiLv.setText(rate_price);
//                                }
//                            } else {
//                                mActivityTradeTvHuiLv.setText(rate_price);
//                            }
//                        }
//
//                        Double Trade_price = Double.valueOf(rate_price);
//                        Double rate = Double.valueOf(rate_price1);
//                        double mul = mul(Trade_price, rate);
//                        Log.i("eiurhgiuehgrue", Trade_price + "==========" + rate_price1);
//                        String s = double2Str(mul);
//
//                        boolean isEngilsh1 = sp.getBoolean("isEngilsh", false);
//                        if (isEngilsh1) {
//                            if (s.contains(",")) {
//                                String replace = s.replace(",", "");
//
//                                if (replace.contains(".")) {
//                                    String[] split = rate_price.split("\\.");
//                                    String ss = split[1];
//                                    String s1 = split[0];
//                                    if (s.length() > Integer.parseInt(mPrice_float_num)) {
//                                        String substring = ss.substring(0, Integer.parseInt(mPrice_float_num));
//                                        String s2 = s1 + "." + substring;
//
//                                        Double doublez = doublez(s2);
//                                        if (doublez < 0.0001) {
//                                            mActivityTradeTvHuiCny.setText("<" + "$" + "0.0001");
//                                        } else {
//                                            mActivityTradeTvHuiCny.setText("≈" + "$" + s2);
//                                        }
//
//                                    } else {
//                                        Double doublez = doublez(replace);
//                                        if (doublez < 0.0001) {
//                                            mActivityTradeTvHuiCny.setText("<" + "$" + "0.0001");
//                                        } else {
//                                            mActivityTradeTvHuiCny.setText("≈" + "$" + replace);
//                                        }
//
//                                    }
//                                } else {
//                                    Double doublez = doublez(replace);
//                                    if (doublez < 0.0001) {
//                                        mActivityTradeTvHuiCny.setText("<" + "$" + "0.0001");
//                                    } else {
//                                        mActivityTradeTvHuiCny.setText("≈" + "$" + replace);
//                                    }
//
//                                }
//
//                            } else {
//
//                                if (s.contains(".")) {
//                                    String[] split = s.split("\\.");
//                                    String ss = split[1];
//                                    String s1 = split[0];
//                                    if (ss.length() > Integer.parseInt(mPrice_float_num)) {
//                                        String substring = ss.substring(0, Integer.parseInt(mPrice_float_num));
//                                        String s2 = s1 + "." + substring;
//                                        Double doublez = doublez(s2);
//                                        if (doublez < 0.0001) {
//                                            mActivityTradeTvHuiCny.setText("<" + "$" + "0.0001");
//                                        } else {
//                                            mActivityTradeTvHuiCny.setText("≈" + "$" + s2);
//                                        }
//
//                                    } else {
//                                        Double doublez = doublez(s);
//                                        if (doublez < 0.0001) {
//                                            mActivityTradeTvHuiCny.setText("<" + "$" + "0.0001");
//                                        } else {
//                                            mActivityTradeTvHuiCny.setText("≈" + "$" + s);
//                                        }
//
//                                    }
//                                } else {
//                                    Double doublez = doublez(s);
//                                    if (doublez < 0.0001) {
//                                        mActivityTradeTvHuiCny.setText("<" + "$" + "0.0001");
//                                    } else {
//                                        mActivityTradeTvHuiCny.setText("≈" + "$" + s);
//                                    }
//
//                                }
//                            }
//
//                        } else {
//
//                            if (s.contains(",")) {
//                                String replace = s.replace(",", "");
//                                Log.i("kwehfkwuheifuhwiehfiw", replace);
//                                if (replace.contains(".")) {
//                                    String[] split = replace.split("\\.");
//                                    String ss = split[1];
//                                    String s1 = split[0];
//                                    if (s.length() > Integer.parseInt(mPrice_float_num)) {
//                                        String substring = ss.substring(0, Integer.parseInt(mPrice_float_num));
//                                        String s2 = s1 + "." + substring;
//
//                                        Double doublez = doublez(s2);
//                                        if (doublez < 0.0001) {
//                                            mActivityTradeTvHuiCny.setText("<" + "￥" + "0.0001");
//                                        } else {
//                                            mActivityTradeTvHuiCny.setText("≈" + "￥" + s2);
//                                        }
//
//                                    } else {
//                                        Double doublez = doublez(replace);
//                                        if (doublez < 0.0001) {
//                                            mActivityTradeTvHuiCny.setText("<" + "￥" + "0.0001");
//                                        } else {
//                                            mActivityTradeTvHuiCny.setText("≈" + "￥" + replace);
//                                        }
//
//                                    }
//                                } else {
//
//                                    Double doublez = doublez(replace);
//                                    if (doublez < 0.0001) {
//                                        mActivityTradeTvHuiCny.setText("<" + "￥" + "0.0001");
//                                    } else {
//                                        mActivityTradeTvHuiCny.setText("≈" + "￥" + replace);
//                                    }
//
//                                }
//
////                                mActivityTradeTvHuiCny.setText("≈" + "￥" + replace.substring(0, Integer.parseInt(mPrice_float_num)));
//                            } else {
//                                if (s.contains(".")) {
//                                    String[] split = s.split("\\.");
//                                    String ss = split[1];
//                                    String s1 = split[0];
//                                    if (s.length() > Integer.parseInt(mPrice_float_num)) {
//                                        String substring = ss.substring(0, Integer.parseInt(mPrice_float_num));
//                                        String s2 = s1 + "." + substring;
//                                        Log.i("lwjfowieofjwie", s2);
//                                        Double doublez = doublez(s2);
//                                        if (doublez < 0.0001) {
//                                            mActivityTradeTvHuiCny.setText("<" + "￥" + "0.0001");
//                                        } else {
//                                            mActivityTradeTvHuiCny.setText("≈" + "￥" + s2);
//                                        }
//
//                                    } else {
//                                        Double doublez = doublez(s);
//                                        if (doublez < 0.0001) {
//                                            mActivityTradeTvHuiCny.setText("<" + "￥" + "0.0001");
//                                        } else {
//                                            mActivityTradeTvHuiCny.setText("≈" + "￥" + s);
//                                        }
//
//                                    }
//                                } else {
//                                    Double doublez = doublez(s);
//
//
//                                    if (doublez < 0.0001) {
//                                        mActivityTradeTvHuiCny.setText("<" + "￥" + "0.0001");
//                                    } else {
//                                        mActivityTradeTvHuiCny.setText("≈" + "￥" + s);
//                                    }
//
//                                }
//
////                                mActivityTradeTvHuiCny.setText("≈" + "￥" + replace.substring(0, Integer.parseInt(mPrice_float_num)));
//                            }
//
//                        }
//
//
//                    }
//                } catch (JSONException e) {
//                    e.printStackTrace();
//
//                }
//
//
//            }
//
//            @Override
//            public void onFailure(HttpException error, String msg) {
//
//            }
//        });
    }

    private void setJiaJian(String price_float_num) {
        Integer integer = Integer.valueOf(price_float_num);
        if (integer == 1) {
            jiajian = 0.1;
        } else if (integer == 2) {
            jiajian = 0.01;
        } else if (integer == 3) {
            jiajian = 0.001;
        } else if (integer == 4) {
            jiajian = 0.0001;
        } else if (integer == 5) {
            jiajian = 0.00001;
        } else if (integer == 6) {
            jiajian = 0.000001;
        } else if (integer == 7) {
            jiajian = 0.0000001;
        } else if (integer == 8) {
            jiajian = 0.00000001;
        } else if (integer == 9) {
            jiajian = 0.000000001;
        }

    }

    //设置涨跌停
    private void setShowZhangDie(TexchangeRateInfo texchangeRateInfo) {


        if (texchangeRateInfo.getData().getLimit_up() != null) {

            boolean isEngilsh = sp.getBoolean("isEngilsh", false);
            String limit_down = texchangeRateInfo.getData().getLimit_down();
            double v = Double.parseDouble(limit_down);
            if (v > 0) {

                if (isEngilsh) {
                    mTradeactivityTvDie.setText(texchangeRateInfo.getData().getLimit_down());
                    mTradeactivityTvZhang.setText(texchangeRateInfo.getData().getLimit_up());
                    mTradeactivityRelaZhangdie.setVisibility(View.VISIBLE);
                    mTradeProgessZhangDie.setVisibility(View.VISIBLE);

                } else {
                    mTradeactivityTvDie.setText(getString(R.string.ground_limit_price) + "\t" + texchangeRateInfo.getData().getLimit_down());
                    mTradeactivityTvZhang.setText(texchangeRateInfo.getData().getLimit_up() + "\t" + getString(R.string.top_limit_price));
                    mTradeactivityRelaZhangdie.setVisibility(View.VISIBLE);
                    mTradeProgessZhangDie.setVisibility(View.VISIBLE);
                }


            } else {
                mTradeactivityRelaZhangdie.setVisibility(View.GONE);
                mTradeProgessZhangDie.setVisibility(View.GONE);

            }

        }

    }

    private void getUserMoneyInfo(String type, String s) {


        OkGo.<String>post(Config.URL_ENTRUST_INFO).tag(this).params("type", type).params("danwei", s).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                String result = response.body().toString();

                if (mMicroAVLIDialog != null) {
                    mMicroAVLIDialog.dismiss();
                }

                Log.i("kjwehfkwuhfuiwef", result);
                try {
                    JSONObject jsonObject = new JSONObject(result);

                    if ("1".equals(jsonObject.getString(Config.KEY_CODE))) {

                        Gson gson = new Gson();
                        mUserMoneyInfo = gson.fromJson(result, UserMoneyInfo.class);
                        setUserInfo(mUserMoneyInfo);

                    } else {

                    }
                } catch (JSONException e) {

                }
            }
        });


//        RequestParams params = new RequestParams();
//        params.addBodyParameter("type", type);
//        params.addBodyParameter("danwei", s);
//
//        cookieStore = CookieTool.getCookieStore(this, sp);
//        new HttpUtils().configCookieStore(cookieStore).send(HttpRequest.HttpMethod.POST, Config.URL_ENTRUST_INFO, params, new RequestCallBack<String>() {
//            @Override
//            public void onSuccess(ResponseInfo<String> responseInfo) {
//
//                mMicroAVLIDialog.dismiss();
//                String result = responseInfo.result.toString();
////                Log.i("kjwehfkwuhfuiwef", result);
//                try {
//                    JSONObject jsonObject = new JSONObject(result);
//
//                    if ("1".equals(jsonObject.getString(Config.KEY_CODE))) {
//
//                        Gson gson = new Gson();
//                        mUserMoneyInfo = gson.fromJson(result, UserMoneyInfo.class);
//                        setUserInfo(mUserMoneyInfo);
//
//                    } else {
//
//                    }
//                } catch (JSONException e) {
//
//                }
//            }
//
//            @Override
//            public void onFailure(HttpException error, String msg) {
//
//            }
//
//        });


    }

    private void setUserInfo(UserMoneyInfo userMoneyInfo) {

        String can_buy_max = userMoneyInfo.getData().getBalance().getDanwei();
        String can_sell_max = userMoneyInfo.getData().getBalance().getType();


        if (isBuy) {
            mExplainTypValue.setText(can_buy_max.trim());
            mExplainTypMoney.setText(mDanwei);
        } else {
            mExplainTypValue.setText(can_sell_max.trim());
            mExplainTypMoney.setText(mType);
        }


    }

    @Override
    protected void onResume() {
        super.onResume();

        sp = SharedPreferencesUtil.getSharedPreferences(this);
        boolean isEngilsh = sp.getBoolean("isEngilsh", false);
        String token = sp.getString(Config.TOKEN, null);
        //        预加载webview
        if (!TextUtils.isEmpty(typePair)) {
            if (isEngilsh) {

                mExtraHeaders = new HashMap<String, String>();
                mExtraHeaders.put("Cookie", token);
                mExtraHeaders.put("Token", token);
                mExtraHeaders.put("SESSID", token);
                mExtraHeaders.put("myversion", Tools.getVersion(this));
                mExtraHeaders.put("myclient", "android");
                mExtraHeaders.put("lang", "en");
                urls = Config.URL + "home/orders/" + typePair + "?lang=en";
            } else {
                mExtraHeaders = new HashMap<String, String>();
                mExtraHeaders.put("Cookie", token);
                mExtraHeaders.put("Token", token);
                mExtraHeaders.put("SESSID", token);
                mExtraHeaders.put("myversion", Tools.getVersion(this));
                mExtraHeaders.put("myclient", "android");
                mExtraHeaders.put("lang", "zh_cn");
                urls = Config.URL + "home/orders/" + typePair + "?lang=zh_cn";
            }
        }


        mActivityWebview.loadUrl(urls, mExtraHeaders);
        mIsFirst = sp.getBoolean("isFirst", false);
        mIs_login = sp.getBoolean("is_login", false);

        String checkTrade = sp.getString("checkTrade", "0");
        Integer integer = Integer.valueOf(checkTrade);
        ((RadioButton) (mSwitchButton.getChildAt(integer))).setChecked(true);

        if (mIs_login) {
            if (mIsFirst) {
                mActivityTradePsw.setVisibility(View.GONE);
            } else {
                mActivityTradePsw.setVisibility(View.VISIBLE);
            }
            mBtnConfirm.setVisibility(View.VISIBLE);
            mActivityBButDz.setVisibility(View.GONE);
        } else {

            if (mIs_login) {
                mActivityTradePsw.setVisibility(View.GONE);
                mBtnConfirm.setVisibility(View.GONE);
            }
        }
        isShow = true;
        handler.removeCallbacks(runnable);
        handler.postDelayed(runnable, 0);

    }

    private void initButton() {
        if (mIs_login) {
            mActivityBButDz.setVisibility(View.GONE);
            mBtnConfirm.setVisibility(View.VISIBLE);

        } else {
            mActivityBButDz.setVisibility(View.VISIBLE);
            mBtnConfirm.setVisibility(View.GONE);
        }
    }

    private Handler handler = new Handler() {

    };
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {

            if (isShow) {
                getOrderbook();
                getTradesInfo();
                geTexchangeRate();
                if (mType != null && mDanwei != null) {
                    getUserMoneyInfo(mType, mDanwei);
                }

            }

            handler.postDelayed(this, 5000);
        }
    };

    private void initRecyclerview() {

    }

    @Override
    public void setData() {

    }


    @Override
    public void onClick(View view) {

    }

    public void getOrderbook() {

        int num = (int) ((Math.random() * 9 + 1) * 100000);

        OkGo.<String>get(Config.URL_ORDERBOOK + "?rnd=" + String.valueOf(num)).params("pair", typePair).params("depth", "5").tag(this).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                String result = response.body().toString();
                try {
                    JSONObject jsonObj = new JSONObject(result);
                    if (jsonObj.getString("code").equals("1")) {
                        Gson gson = new Gson();
                        OrderBookInfo orderBookInfo = gson.fromJson(result, OrderBookInfo.class);
                        ArrayList<OrderBookInfo.DataBean.BuyBean> buy = orderBookInfo.getData().getBuy();
                        ArrayList<OrderBookInfo.DataBean.SellBean> sell = orderBookInfo.getData().getSell();
                        initRecyData(buy, sell);

                    }
                } catch (JSONException e) {
                    e.printStackTrace();

                }
            }
        });


//        RequestParams params = new RequestParams();
//        params.addQueryStringParameter("pair", typePair);
//        params.addQueryStringParameter("depth", "5");
//
//        new HttpUtils().send(HttpRequest.HttpMethod.GET, Config.URL_ORDERBOOK + "?rnd=" + String.valueOf(num), params, new RequestCallBack<String>() {
//            @Override
//            public void onSuccess(ResponseInfo<String> responseInfo) {
//
//                String result = responseInfo.result;
//                Gson gson = new Gson();
//                OrderBookInfo orderBookInfo = gson.fromJson(result, OrderBookInfo.class);
//                ArrayList<OrderBookInfo.DataBean.BuyBean> buy = orderBookInfo.getData().getBuy();
//
//                ArrayList<OrderBookInfo.DataBean.SellBean> sell = orderBookInfo.getData().getSell();
//
//                initRecyData(buy, sell);
//
//
//            }
//
//            @Override
//            public void onFailure(HttpException error, String msg) {
//
//
//            }
//        });

    }

    private void initRecyData(ArrayList<OrderBookInfo.DataBean.BuyBean> buy, ArrayList<OrderBookInfo.DataBean.SellBean> sell) {

        Collections.sort(buy, new Comparator<OrderBookInfo.DataBean.BuyBean>() {
            @Override
            public int compare(OrderBookInfo.DataBean.BuyBean buyBean, OrderBookInfo.DataBean.BuyBean t1) {

                String amount = buyBean.getAmount();
                String amount1 = t1.getAmount();
                Double aDouble = Double.valueOf(amount);
                Double aDouble1 = Double.valueOf(amount1);


                if (aDouble > aDouble1) {
                    return -1;
                } else if (aDouble < aDouble1) {
                    return 1;
                } else {
                    return 0;
                }

            }
        });


        Collections.sort(sell, new Comparator<OrderBookInfo.DataBean.SellBean>() {
            @Override
            public int compare(OrderBookInfo.DataBean.SellBean sellBean, OrderBookInfo.DataBean.SellBean t1) {

                String amount = sellBean.getAmount();
                String amount1 = t1.getAmount();
                Double aDouble = Double.valueOf(amount);
                Double aDouble1 = Double.valueOf(amount1);

                if (aDouble > aDouble1) {
                    return -1;
                } else if (aDouble < aDouble1) {
                    return 1;
                } else {
                    return 0;
                }

            }
        });

        String max = "0";
        String s = "";


        if (buy.size() != 0) {

            String amount = buy.get(0).getAmount();
            Log.i("123456", amount);
            String[] split = amount.split("\\.");
//            String s = split[0] + split[1];
            s = split[0];

            if (Integer.valueOf(s) == 0) {
                s = split[0] + "." + split[1];

                if (sell.size() != 0) {

                    String amount1 = sell.get(0).getAmount();
                    String[] split1 = amount1.split("\\.");
                    String s1 = split1[0];
                    if (Integer.valueOf(s1) == 0) {
                        mS1 = split1[0] + "." + split1[1];
                    } else {
//                        mS1 = split1[0];
                        mS1 = split1[0] + "." + split1[1];
                    }
//                mS1 = split1[0] + split1[1];


                }


            } else {

                s = split[0] + "." + split[1];
                if (sell.size() != 0) {

                    String amount1 = sell.get(0).getAmount();
                    String[] split1 = amount1.split("\\.");
                    String s1 = split1[0];
                    if (Integer.valueOf(s1) == 0) {
                        mS1 = split1[0] + "." + split1[1];

                    } else {
//                        mS1 = split1[0];
                        mS1 = split1[0] + "." + split1[1];

                    }
//                mS1 = split1[0] + split1[1];


                }

            }


            Double up = Double.valueOf(s);
            Double down = Double.valueOf(mS1);

            if (up > down) {

                max = s;
            } else {
                max = mS1;
            }


            Collections.sort(buy, new Comparator<OrderBookInfo.DataBean.BuyBean>() {
                @Override
                public int compare(OrderBookInfo.DataBean.BuyBean buyBean, OrderBookInfo.DataBean.BuyBean t1) {

                    String amount = buyBean.getPrice();
                    String amount1 = t1.getPrice();
                    Double aDouble = Double.valueOf(amount);
                    Double aDouble1 = Double.valueOf(amount1);


                    if (aDouble > aDouble1) {

                        return -1;

                    } else if (aDouble < aDouble1) {

                        return 1;
                    } else {

                        return 0;
                    }

                }
            });


//            Log.i("kefhwiuehfwief", max);
//            3807109107
            //       买盘
            mActivityHeaderTradeDown.setLayoutManager(new LinearLayoutManager(this));

            TradeDownAdapter tradeDownAdapter = new TradeDownAdapter(null, max);
            mActivityHeaderTradeDown.setAdapter(tradeDownAdapter);
            tradeDownAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    OrderBookInfo.DataBean.BuyBean buyBean = (OrderBookInfo.DataBean.BuyBean) adapter.getData().get(position);
                    String buyBeanAmount = buyBean.getAmount();
                    String price = buyBean.getPrice();
                    mEtPrice.setText(price);
                    mEtNum.setText(buyBeanAmount);

                }
            });

            tradeDownAdapter.setNewData(buy);

        }

        if (sell.size() != 0) {

            if (buy.size() != 0) {
                String amount = buy.get(0).getAmount();

                String[] split = amount.split("\\.");
//            String s = split[0] + split[1];
                mS = split[0];
            }

            String amount1 = sell.get(0).getAmount();
            String[] split1 = amount1.split("\\.");
//            String s1 = split1[0] + split1[1];
            String s1 = split1[0];

            Double up = Double.valueOf(mS);
            Double down = Double.valueOf(s1);


            Collections.sort(sell, new Comparator<OrderBookInfo.DataBean.SellBean>() {
                @Override
                public int compare(OrderBookInfo.DataBean.SellBean sellBean, OrderBookInfo.DataBean.SellBean t1) {

                    String amount = sellBean.getPrice();
                    String amount1 = t1.getPrice();
                    Double aDouble = Double.valueOf(amount);
                    Double aDouble1 = Double.valueOf(amount1);

                    if (aDouble > aDouble1) {
                        return -1;
                    } else if (aDouble < aDouble1) {
                        return 1;
                    } else {
                        return 0;
                    }

                }
            });


            //        卖盘
            mActivityHeaderTradeUp.setLayoutManager(new LinearLayoutManager(this));
            TradeUpAdapter tradeUpAdapter = new TradeUpAdapter(null, max);

            mActivityHeaderTradeUp.setAdapter(tradeUpAdapter);
            tradeUpAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    OrderBookInfo.DataBean.SellBean sellBean = (OrderBookInfo.DataBean.SellBean) adapter.getData().get(position);
                    String amount2 = sellBean.getAmount();
                    String price = sellBean.getPrice();
                    mEtPrice.setText(price);
                    mEtNum.setText(amount2);


                }
            });

            tradeUpAdapter.setNewData(sell);

        }


//        卖盘
        mActivityHeaderTradeUp.setLayoutManager(new LinearLayoutManager(this));


    }

    @OnClick({R.id.activity_trade_kine_imag, R.id.btnConfirm, R.id.activity_b_but_dz, R.id.activity_trade_jian, R.id.activity_trade_jia, R.id.ll_history_entrust, R.id.ll_trade_history, R.id.avtivity_trade_look_more})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.activity_trade_kine_imag:

                Intent intent = new Intent(this, KLineActivity.class);
                intent.putExtra("pair", typePair);
                startActivity(intent);
                break;
            case R.id.btnConfirm:
                String trim2 = mEtPrice.getText().toString().trim();
                if (TextUtils.isEmpty(trim2)) {
//                    SnackbarShow(getString(R.string.price_null));
                    Util.showSnachBar(TradeActivityss.this, getResources().getColor(R.color.red), getString(R.string.price_null));
                    return;
                }

                String trim3 = mEtNum.getText().toString().trim();
                if (TextUtils.isEmpty(trim3)) {
//                    SnackbarShow(getString(R.string.number_null));
                    Util.showSnachBar(TradeActivityss.this, getResources().getColor(R.color.red), getString(R.string.number_null));
                    return;
                }
                String trim1 = mEtTradePsw.getText().toString().trim();
                if (TextUtils.isEmpty(trim1)) {

//                    return;
                }

                if (mIs_mai) {

                    showBuyDialog(trim3, trim2, trim1, true);
                } else {

                    showBuyDialog(trim3, trim2, trim1, false);
                }

                String ww = mBtnConfirm.getText().toString();


                break;
            case R.id.activity_b_but_dz:

                Intent i = new Intent(cxt, LoginActivity.class);
                startActivity(i);
                break;

            case R.id.activity_trade_jian:

                String trim = mEtPrice.getText().toString().trim();
                boolean empty = TextUtils.isEmpty(trim);
                if (empty || trim.equals("0.000000")) {
                    return;
                }

                d -= jiajian;

                String s1 = double2Str(d);
                mEtPrice.setText(s1);

                break;
            case R.id.activity_trade_jia:

                d += jiajian;
                String s = double2Str(d);
                mEtPrice.setText(s);

                break;

//            我的历史委托
            case R.id.ll_history_entrust:

                Intent ii = new Intent(cxt, WebActivity.class);
//                ii.putExtra("web_url", Config.URL + "home/orders/" + typePair);
                ii.putExtra("web_url", Config.URL + "home/history_orders/" + typePair);

                startActivity(ii);
                break;
//            成交历史
            case R.id.ll_trade_history:

                Intent intent1 = new Intent(cxt, WebActivity.class);
//                intent1.putExtra("web_url", Config.URL + "home/markets/trade/" + typePair);
                intent1.putExtra("web_url", Config.URL + "home/trade_m/" + typePair);
                startActivity(intent1);

                break;

            case R.id.avtivity_trade_look_more:

                Intent in = new Intent(cxt, WebActivity.class);
                in.putExtra("web_url", Config.URL + "trade/trade_more?pair=" + typePair);
                startActivity(in);
                break;
        }
    }

    public void getTradesInfo() {
        final int num = (int) ((Math.random() * 9 + 1) * 100000);

        OkGo.<String>get(Config.URL_PAIR_HISTORY + "?rnd=" + String.valueOf(num)).params("pair", typePair).tag(this).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                String result = response.body().toString();
                try {
                    JSONObject jsonObj = new JSONObject(result);
                    if (jsonObj.getString("code").equals("1")) {
                        Gson gson = new Gson();
                        TradeInfo tradeInfo = gson.fromJson(result, TradeInfo.class);
                        List<TradeInfo.DataBean.ListBean> list = tradeInfo.getData().getList();
                        mActivityTradeRecy.setLayoutManager(new LinearLayoutManager(TradeActivityss.this));
                        TradesAdapter tradesAdapter = new TradesAdapter(null);
                        if (!TextUtils.isEmpty(mPrice_float_num)) {
                            tradesAdapter.setPriceNumber(mPrice_float_num);
                        }

                        if (!TextUtils.isEmpty(mFloat_num)) {
                            tradesAdapter.setAumontNumber(mFloat_num);
                        }
                        tradesAdapter.setNewData(list);
                        mActivityTradeRecy.setAdapter(tradesAdapter);

                    }
                } catch (JSONException e) {
                    e.printStackTrace();

                }
            }
        });


        RequestParams params = new RequestParams();
        params.addQueryStringParameter("pair", typePair);

        new HttpUtils().send(HttpRequest.HttpMethod.GET, Config.URL_PAIR_HISTORY + "?rnd=" + String.valueOf(num), params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {

                String result = responseInfo.result;

                Gson gson = new Gson();
                TradeInfo tradeInfo = gson.fromJson(result, TradeInfo.class);
                List<TradeInfo.DataBean.ListBean> list = tradeInfo.getData().getList();
                mActivityTradeRecy.setLayoutManager(new LinearLayoutManager(TradeActivityss.this));
                TradesAdapter tradesAdapter = new TradesAdapter(null);
                if (!TextUtils.isEmpty(mPrice_float_num)) {
                    tradesAdapter.setPriceNumber(mPrice_float_num);
                }

                if (!TextUtils.isEmpty(mFloat_num)) {
                    tradesAdapter.setAumontNumber(mFloat_num);
                }

                tradesAdapter.setNewData(list);
                mActivityTradeRecy.setAdapter(tradesAdapter);


            }

            @Override
            public void onFailure(HttpException error, String msg) {


            }
        });
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, @IdRes int checkedId) {

        switch (checkedId) {
            case R.id.tab1:
                sp.putString("checkTrade", "0");
                initRadioButton();
                initEdit();
                mActivityWebview.setVisibility(View.GONE);
                mIs_mai = true;
                initRadbutionColorBuy();
                isBuy = true;
                mLlBuySell.setVisibility(View.VISIBLE);
                mLlHistory.setVisibility(View.GONE);
                mActivityTradeKInfo.setVisibility(View.VISIBLE);
                setBuyText();
                setLineBG(1);
                mBtnConfirm.setBackgroundResource(R.drawable.item_bg_no);
                mExplainType.setText(getString(R.string.available_rmb));
                mBtnConfirm.setText(getString(R.string.corn_buy) + " " + mType);
                if (mUserMoneyInfo != null) {
                    setUserInfo(mUserMoneyInfo);
                }


                break;
            case R.id.tab2:
                sp.putString("checkTrade", "1");
                initRadioButton();
                initEdit();
                mIs_mai = false;
                isBuy = false;
                initRadbutionColor();
                mActivityWebview.setVisibility(View.GONE);
                mLlBuySell.setVisibility(View.VISIBLE);
                mLlHistory.setVisibility(View.GONE);
                mActivityTradeKInfo.setVisibility(View.VISIBLE);
                setLineBG(2);
                mExplainType.setText(getString(R.string.available_rmb));
                mBtnConfirm.setText(getString(R.string.corn_sell) + " " + mType);
                mBtnConfirm.setBackgroundResource(R.drawable.item_bg);
                if (mUserMoneyInfo != null) {
                    setUserInfo(mUserMoneyInfo);
                }
                break;
            case R.id.tab3:
                mTradeProgessZhangDie.setVisibility(View.GONE);
                mTradeactivityRelaZhangdie.setVisibility(View.GONE);
                boolean aBoolean = sp.getBoolean(Config.KEY_IS_LOGIN, false);
                if (aBoolean) {
                    setLineBG(3);
                    mActivityWebview.setVisibility(View.VISIBLE);
                    mLlBuySell.setVisibility(View.GONE);
                    mLlHistory.setVisibility(View.VISIBLE);
                    mActivityTradeKInfo.setVisibility(View.GONE);
                } else {
                    Intent intent = new Intent(this, LoginActivity.class);
                    startActivity(intent);
                    finish();
                }
//                setLineBG(3);
//                mActivityWebview.setVisibility(View.VISIBLE);
//                mLlBuySell.setVisibility(View.GONE);
//                mLlHistory.setVisibility(View.VISIBLE);
//                mActivityTradeKInfo.setVisibility(View.GONE);
                break;

//            0%
            case R.id.activity_reabution_one:


                if (isBuy) {
                    String activity_reabution_fine = mExplainTypValue.getText().toString().trim();
                    String trim = mEtPrice.getText().toString().trim();
                    if (!TextUtils.isEmpty(trim)) {
                        Double aDouble1 = Double.valueOf(trim);

                        if (!TextUtils.isEmpty(activity_reabution_fine)) {
                            if (activity_reabution_fine.contains(",")) {
                                String replace = activity_reabution_fine.replace(",", "");
                                Double aDouble = Double.valueOf(replace);
                                double div = div(aDouble, aDouble1, Integer.valueOf(mPrice_float_num));
                                double mul = mul(div, 0.0);
                                String s = double2Str(mul);
                                mEtNum.setText(s);

                            } else {

                                Double aDouble = Double.valueOf(activity_reabution_fine);
                                double div = div(aDouble, aDouble1, 6);
                                double mul = mul(div, 0.0);
                                String s = double2Str(mul);
                                mEtNum.setText(s);
                            }
                        }
                    }
                } else {
                    String activity_reabution_fine = mExplainTypValue.getText().toString().trim();
                    if (!TextUtils.isEmpty(activity_reabution_fine)) {
                        Double mai = Double.valueOf(activity_reabution_fine);
                        double mul = mul(mai, 0.0);
                        String s = double2Str(mul);
                        mEtNum.setText(s);

                    }
                }

                break;
//            25%
            case R.id.activity_reabution_two:


                if (isBuy) {
                    String activity_reabution_fine = mExplainTypValue.getText().toString().trim();
                    String trim = mEtPrice.getText().toString().trim();
                    if (!TextUtils.isEmpty(trim)) {
                        Double aDouble1 = Double.valueOf(trim);

                        if (!TextUtils.isEmpty(activity_reabution_fine)) {
                            if (activity_reabution_fine.contains(",")) {
                                String replace = activity_reabution_fine.replace(",", "");
                                Double aDouble = Double.valueOf(replace);
                                double div = div(aDouble, aDouble1, Integer.valueOf(mPrice_float_num));
                                double mul = mul(div, 0.25);
                                String s = double2Str(mul);
                                mEtNum.setText(s);

                            } else {
                                Double aDouble = Double.valueOf(activity_reabution_fine);
                                double div = div(aDouble, aDouble1, 6);

                                double mul = mul(div, 0.25);
                                String s = double2Str(mul);
                                mEtNum.setText(s);
                            }
                        }
                    }
                } else {
                    String activity_reabution_fine = mExplainTypValue.getText().toString().trim();
                    if (!TextUtils.isEmpty(activity_reabution_fine)) {
                        Double mai = Double.valueOf(activity_reabution_fine);
                        double mul = mul(mai, 0.25);
                        String s = double2Str(mul);
                        mEtNum.setText(s);

                    }
                }

                break;
//            50%
            case R.id.activity_reabution_three:

                if (isBuy) {
                    String activity_reabution_fine = mExplainTypValue.getText().toString().trim();
                    String trim = mEtPrice.getText().toString().trim();
                    if (!TextUtils.isEmpty(trim)) {
                        Double aDouble1 = Double.valueOf(trim);

                        if (!TextUtils.isEmpty(activity_reabution_fine)) {
                            if (activity_reabution_fine.contains(",")) {
                                String replace = activity_reabution_fine.replace(",", "");
                                Double aDouble = Double.valueOf(replace);
                                double div = div(aDouble, aDouble1, Integer.valueOf(mPrice_float_num));
                                double mul = mul(div, 0.5);
                                String s = double2Str(mul);
                                mEtNum.setText(s);

                            } else {
                                Double aDouble = Double.valueOf(activity_reabution_fine);
                                double div = div(aDouble, aDouble1, 6);
                                double mul = mul(div, 0.5);
                                String s = double2Str(mul);
                                mEtNum.setText(s);
                            }
                        }
                    }
                } else {

                    String activity_reabution_fine = mExplainTypValue.getText().toString().trim();
                    if (!TextUtils.isEmpty(activity_reabution_fine)) {
                        Double mai = Double.valueOf(activity_reabution_fine);
                        double mul = mul(mai, 0.50);
                        String s = double2Str(mul);
                        mEtNum.setText(s);
                    }
                }

                break;
//            75%
            case R.id.activity_reabution_four:

                if (isBuy) {
                    String activity_reabution_fine = mExplainTypValue.getText().toString().trim();
                    String trim = mEtPrice.getText().toString().trim();
                    if (!TextUtils.isEmpty(trim)) {
                        Double aDouble1 = Double.valueOf(trim);

                        if (!TextUtils.isEmpty(activity_reabution_fine)) {
                            if (activity_reabution_fine.contains(",")) {
                                String replace = activity_reabution_fine.replace(",", "");
                                Double aDouble = Double.valueOf(replace);
                                double div = div(aDouble, aDouble1, Integer.valueOf(mPrice_float_num));
                                double mul = mul(div, 0.75);
                                String s = double2Str(mul);
                                mEtNum.setText(s);

                            } else {
                                Double aDouble = Double.valueOf(activity_reabution_fine);
                                double div = div(aDouble, aDouble1, 6);
                                double mul = mul(div, 0.75);
                                String s = double2Str(mul);
                                mEtNum.setText(s);
                            }
                        }
                    }
                } else {
                    String activity_reabution_fine = mExplainTypValue.getText().toString().trim();
                    if (!TextUtils.isEmpty(activity_reabution_fine)) {
                        Double mai = Double.valueOf(activity_reabution_fine);
                        double mul = mul(mai, 0.75);
                        String s = double2Str(mul);
                        mEtNum.setText(s);

                    }
                }

                break;
//            100%
            case R.id.activity_reabution_fine:

                if (isBuy) {
                    String activity_reabution_fine = mExplainTypValue.getText().toString().trim();
                    String trim = mEtPrice.getText().toString().trim();
                    if (!TextUtils.isEmpty(trim)) {
                        Double aDouble1 = Double.valueOf(trim);

                        if (!TextUtils.isEmpty(activity_reabution_fine)) {
                            if (activity_reabution_fine.contains(",")) {
                                String replace = activity_reabution_fine.replace(",", "");
                                Double aDouble = Double.valueOf(replace);
                                double div = div(aDouble, aDouble1, Integer.valueOf(mPrice_float_num));
                                double mul = mul(div, 1);
                                String s = double2Str(mul);
                                mEtNum.setText(s);

                            } else {
                                Double aDouble = Double.valueOf(activity_reabution_fine);
                                double div = div(aDouble, aDouble1, 6);
                                double mul = mul(div, 1);
                                String s = double2Str(mul);
                                mEtNum.setText(s);
                            }
                        }
                    }
                } else {
                    String activity_reabution_fine = mExplainTypValue.getText().toString().trim();
                    if (!TextUtils.isEmpty(activity_reabution_fine)) {
                        Double mai = Double.valueOf(activity_reabution_fine);
                        double mul = mul(mai, 1.0);
                        String s = double2Str(mul);
                        mEtNum.setText(s);

                    }
                }

                break;

        }

    }

    private void setBuyText() {

        mTvPrice.setText(getString(R.string.corn_buy_price));

    }

    private void setLineBG(int i) {
        mBg1.setBackgroundResource(R.color.bg_gray);
        mBg2.setBackgroundResource(R.color.bg_gray);
        mBg3.setBackgroundResource(R.color.bg_gray);

        if (i == 1)
            mBg1.setBackgroundResource(R.color.bg_green);
        else if (i == 2)
            mBg2.setBackgroundResource(R.color.red);
        else if (i == 3)
            mBg3.setBackgroundResource(R.color.app_color);
    }

    @OnClick(R.id.iv_back)
    public void onViewClicked() {
        finish();
        sp.putString("checkTrade", "0");
    }


    //    double转换
    public String double2Str(Double d) {
        DecimalFormat decimalFormat = new DecimalFormat("#,##0.00000000");// 格式化设置
        String format = decimalFormat.format(d);
        String trim = Tools.round(d, 6).toString().trim();

        return trim;

    }

    public String doubleStr(Double d) {
        DecimalFormat decimalFormat = new DecimalFormat("#,##0.00");// 格式化设置
        String format = decimalFormat.format(d);
        return format;
    }

    /**
     * 虚拟币交易
     *
     * @param price
     * @param isBUy
     */
    public void showBuyDialog(final String num, final String price, final String psw, final boolean isBUy) {
        if (token == null) {
            Intent i = new Intent(cxt, LoginActivity.class);
            startActivity(i);
            return;
        }
        View view = LayoutInflater.from(this).inflate(R.layout.dialog_trade_buy_sell, null);
        final Dialog dialog = new Dialog(this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCancelable(true);// 是否用“返回键”取消
        dialog.setContentView(view);
        TextView tv_content = (TextView) view.findViewById(R.id.tv_dialog_content);
        TextView tv_confirm = (TextView) view.findViewById(R.id.tv_confirm);
        TextView tv_cancel = (TextView) view.findViewById(R.id.tv_cancel);
        TextView tv_dialog_title = (TextView) view.findViewById(R.id.tv_dialog_title);
        String buy_sell;
        if (isBUy) {
            tv_dialog_title.setText(R.string.que_ren);
            tv_confirm.setText(R.string.corn_buy);
            buy_sell = getString(R.string.corn_buy);
        } else {
            tv_dialog_title.setText(R.string.que_ren_mai);
            tv_confirm.setText(R.string.corn_sell);
            buy_sell = getString(R.string.corn_sell);
        }

        tv_content.setText(getString(R.string.sure_is) + price + "\t" + mDanwei + getString(R.string.weituoprice) + buy_sell + num + "\t" + mType);
        tv_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {


                toBuy_Sell(num, price, psw, isBUy);
                dialog.dismiss();
            }
        });
        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                dialog.dismiss();
            }
        });
        // 弹出
        dialog.show();
    }


    /**
     * 进行买卖操
     */
    private void toBuy_Sell(String num, String price, String psw, final boolean isBuy) {

        String isEngilsh = "";
        boolean aBoolean = sp.getBoolean("isEngilsh", false);
        if (aBoolean) {
            isEngilsh = "en";
        } else {
            isEngilsh = "zh_cn";
        }

        String nums = "";
        String prices = "";


        final MicroAVLIDialog microAVLIDialog = new MicroAVLIDialog(this);
        microAVLIDialog.show();
        String url;
        if (isBuy) {

            url = Config.URL_ENTRUST_BUY_COIN;
        } else {

            url = Config.URL_ENTRUST_SELL_COIN;
        }

        if (num.contains(".")) {
            String[] split = num.split("\\.");
            String s = split[1];
            String one = split[0];
            if (s.length() > Integer.valueOf(mFloat_num)) {
                String substring = s.substring(0, Integer.valueOf(mFloat_num));
                nums = one + "." + substring;
            } else {
                nums = num;
            }


        } else {
            nums = num;
        }
        if (price.contains(".")) {
            String[] split = price.split("\\.");
            String price1 = split[0];
            String price2 = split[1];
            if (price2.length() > Integer.valueOf(mPrice_float_num)) {
                String substring = price2.substring(0, Integer.valueOf(mPrice_float_num));
                String s = price1 + "." + substring;
                prices = s;
            } else {
                prices = price;
            }
        } else {
            prices = price;
        }


        //        新增,判断
        if (nums.contains(",")) {
            String replace = nums.replace(",", "");
            nums = replace;
        }

        if (prices.contains(",")) {
            String replace = prices.replace(",", "");
            prices = replace;
        }


        RequestParams params = new RequestParams();
        params.addHeader("app-version", Config.VERSION);
        params.addHeader("channel", MyApplication.mApp.getApplicationMetaData("CXD_CHANNEL_ID"));
        params.addHeader("my-client", "android");
        params.addBodyParameter("num", nums);
        params.addBodyParameter("price", prices);
        params.addBodyParameter("trade_psw", psw);
        params.addBodyParameter("type", mType);
        params.addBodyParameter("danwei", mDanwei);
        params.addBodyParameter("lang", isEngilsh);


        cookieStore = CookieTool.getCookieStore(this, sp);
        new HttpUtils().configCookieStore(cookieStore).send(HttpRequest.HttpMethod.POST, url, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String result = responseInfo.result.toString();

                try {
                    JSONObject jsonObject = new JSONObject(result);

                    if ("1".equals(jsonObject.getString(Config.KEY_CODE))) {
                        if (microAVLIDialog != null) {
                            microAVLIDialog.dismiss();
                        }
                        mActivityTradePsw.setVisibility(View.GONE);
                        initWebview();
                        sp.putBoolean("isFirst", true);
                        if (isBuy) {
//                            SnackbarShow(getString(R.string.ok));
                            Util.showSnachBar(TradeActivityss.this, getResources().getColor(R.color.bg_green), getString(R.string.ok));
                            getTradesInfo();
                            getOrderbook();
                            getUserMoneyInfo(mType, mDanwei);
                        } else {
                            getTradesInfo();
                            getOrderbook();
                            getUserMoneyInfo(mType, mDanwei);
                            Util.showSnachBar(TradeActivityss.this, getResources().getColor(R.color.bg_green), getString(R.string.ok_mai));
//                            SnackbarShow(getString(R.string.ok_mai));
                        }

                        setWebview();

                    } else if ("-1".equals(jsonObject.getString(Config.KEY_CODE))) {
                        if (microAVLIDialog != null) {
                            microAVLIDialog.dismiss();
                        }
//                        SnackbarShow(jsonObject.getString("info"));
                        Util.showSnachBar(TradeActivityss.this, getResources().getColor(R.color.red), jsonObject.getString("info"));

                    } else if ("-7".equals(jsonObject.getString(Config.KEY_CODE))) {
                        if (microAVLIDialog != null) {
                            microAVLIDialog.dismiss();
                        }
//                        SnackbarShow(getString(R.string.input_mima));
                        Util.showSnachBar(TradeActivityss.this, getResources().getColor(R.color.red), getString(R.string.input_mima));
                        mActivityTradePsw.setVisibility(View.VISIBLE);
                    } else {

                        if (microAVLIDialog != null) {
                            microAVLIDialog.dismiss();
                        }

//                        SnackbarShow(jsonObject.getString("info"));
                        Util.showSnachBar(TradeActivityss.this, getResources().getColor(R.color.red), jsonObject.getString("info"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();

                }


            }

            @Override
            public void onFailure(HttpException error, String msg) {

            }

        });
    }

    private void setWebview() {
        boolean isEngilsh = sp.getBoolean("isEngilsh", false);
        String token = sp.getString(Config.TOKEN, null);
        //        预加载webview
        if (!TextUtils.isEmpty(typePair)) {
            if (isEngilsh) {

                mExtraHeaders = new HashMap<String, String>();
                mExtraHeaders.put("Cookie", token);
                mExtraHeaders.put("Token", token);
                mExtraHeaders.put("SESSID", token);
                mExtraHeaders.put("myversion", Tools.getVersion(this));
                mExtraHeaders.put("myclient", "android");
                mExtraHeaders.put("lang", "en");
                urls = Config.URL + "home/orders/" + typePair + "?lang=en";
            } else {
                mExtraHeaders = new HashMap<String, String>();
                mExtraHeaders.put("Cookie", token);
                mExtraHeaders.put("Token", token);
                mExtraHeaders.put("SESSID", token);
                mExtraHeaders.put("myversion", Tools.getVersion(this));
                mExtraHeaders.put("myclient", "android");
                mExtraHeaders.put("lang", "zh_cn");
                urls = Config.URL + "home/orders/" + typePair + "?lang=zh_cn";
            }
        }

        mActivityWebview.loadUrl(urls, mExtraHeaders);
    }
//
//    private void SnackbarShow(String ms) {
//
//        Snackbar snackbar = ShortSnackbar(mBtnConfirm, ms, Color.parseColor("#ffffff"), Color.parseColor("#272736"));
//        snackbar.show();
//    }


    void initRadbutionColor() {

        mActivityReabutionOne.setBackground(ContextCompat.getDrawable(this, R.drawable.selector_radio_button_bg));
        mActivityReabutionTwo.setBackground(ContextCompat.getDrawable(this, R.drawable.selector_radio_button_bg));
        mActivityReabutionThree.setBackground(ContextCompat.getDrawable(this, R.drawable.selector_radio_button_bg));
        mActivityReabutionFour.setBackground(ContextCompat.getDrawable(this, R.drawable.selector_radio_button_bg));
        activity_reabution_fine.setBackground(ContextCompat.getDrawable(this, R.drawable.selector_radio_button_bg));

    }

    void initRadbutionColorBuy() {

        mActivityReabutionOne.setBackground(ContextCompat.getDrawable(this, R.drawable.selector_radio_button_sell_bg));
        mActivityReabutionTwo.setBackground(ContextCompat.getDrawable(this, R.drawable.selector_radio_button_sell_bg));
        mActivityReabutionThree.setBackground(ContextCompat.getDrawable(this, R.drawable.selector_radio_button_sell_bg));
        mActivityReabutionFour.setBackground(ContextCompat.getDrawable(this, R.drawable.selector_radio_button_sell_bg));
        activity_reabution_fine.setBackground(ContextCompat.getDrawable(this, R.drawable.selector_radio_button_sell_bg));

    }


    public Double doublez(String s) {

        if (s.contains(",")) {
            String replace = s.replace(",", "");
            Double aDouble = Double.valueOf(replace);
            return aDouble;
        } else {
            Double aDouble = Double.valueOf(s);
            return aDouble;
        }


    }


    @Override
    protected void onPause() {
        super.onPause();
        isShow = false;


    }


    public double div(double d1, double d2, int len) {// 进行除法运算
        BigDecimal b1 = new BigDecimal(d1);
        BigDecimal b2 = new BigDecimal(d2);
        return b1.divide(b2, len, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    public double mul(double d1, double d2) {        // 进行乘法运算
        BigDecimal b1 = new BigDecimal(d1);
        BigDecimal b2 = new BigDecimal(d2);
        return b1.multiply(b2).doubleValue();
    }

    public void initRadioButton() {
        mActivityReabutionOne.setChecked(false);
        mActivityReabutionTwo.setChecked(false);
        mActivityReabutionThree.setChecked(false);
        mActivityReabutionFour.setChecked(false);
        activity_reabution_fine.setChecked(false);
    }

    void initEdit() {
        mEtPrice.setText("");
        mEtNum.setText("");

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            if (keyCode == KeyEvent.KEYCODE_BACK) { //表示按返回键 时的操作
                sp.putString("checkTrade", "0");
            }
        }
        return super.onKeyDown(keyCode, event);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        sp.putString("checkTrade", "0");
        OkGo.getInstance().cancelTag(this);
    }

    private void initWebview() {
        boolean isEngilsh = sp.getBoolean("isEngilsh", false);
        if (isEngilsh) {

            mExtraHeaders = new HashMap<String, String>();
            mExtraHeaders.put("Cookie", token);
            mExtraHeaders.put("Token", token);
            mExtraHeaders.put("SESSID", token);
            mExtraHeaders.put("myversion", Tools.getVersion(this));
            mExtraHeaders.put("myclient", "android");
            mExtraHeaders.put("lang", "en");
            urls = Config.URL + "home/orders/" + typePair + "?lang=en";
        } else {
            mExtraHeaders = new HashMap<String, String>();
            mExtraHeaders.put("Cookie", token);
            mExtraHeaders.put("Token", token);
            mExtraHeaders.put("SESSID", token);
            mExtraHeaders.put("myversion", Tools.getVersion(this));
            mExtraHeaders.put("myclient", "android");
            mExtraHeaders.put("lang", "zh_cn");
            urls = Config.URL + "home/orders/" + typePair + "?lang=zh_cn";
        }

        mActivityWebview.loadUrl(urls, mExtraHeaders);

    }

    void initTextView() {

        mActivityTradeTotalMoney.setText("0.00" + "\t" + mDanwei);

    }

//    public static Snackbar ShortSnackbar(View view, String message, int messageColor, int backgroundColor) {
//        Snackbar snackbar = Snackbar.make(view, message, Snackbar.LENGTH_SHORT);
//        setSnackbarColor(snackbar, messageColor, backgroundColor);
//        return snackbar;
//    }
//
//    /**
//     * 设置Snackbar文字和背景颜色
//     *
//     * @param snackbar
//     * @param messageColor
//     * @param backgroundColor
//     */
//    public static void setSnackbarColor(Snackbar snackbar, int messageColor, int backgroundColor) {
//        View view = snackbar.getView();
//
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
//            TextView message = (TextView) snackbar.getView().findViewById(R.id.snackbar_text);
//            //View.setTextAlignment需要SDK>=17
//            message.setTextAlignment(View.TEXT_ALIGNMENT_GRAVITY);
//            message.setGravity(Gravity.CENTER);
//        }
//
//        if (view != null) {
//
//            view.setBackgroundColor(backgroundColor);
//            ((TextView) view.findViewById(R.id.snackbar_text)).setTextColor(messageColor);
//
//        }
//    }

    //设置EditText可输入和不可输入状态
    private void editTextable(EditText editText, boolean editable) {
//        editText.setFocusableInTouchMode(true);
        if (!editable) { // disable editing password
            editText.setFocusable(false);
            editText.setFocusableInTouchMode(true);
            editText.setClickable(false); // user navigates with wheel and selects widget
        } else { // enable editing of password
            editText.setFocusable(true);
            editText.setFocusableInTouchMode(true);
            editText.setClickable(true);
        }
    }


//    void hideEd(EditText editText) {
//
//
//        InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
//        imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
//    }

    private void setCity() {
        mCity_new = sp.getString("city_new", "0");
        if (mCity_new.equals("0")) {

            //17以上利用 setLocale（）
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                mConfiguration.setLocale(Locale.SIMPLIFIED_CHINESE);
            } else {
                mConfiguration.locale = Locale.SIMPLIFIED_CHINESE;
            }
            mResources.updateConfiguration(mConfiguration, mDisplayMetrics);


        } else if (mCity_new.equals("1")) {

            //17以上利用 setLocale（）
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                mConfiguration.setLocale(Locale.US);
            } else {
                mConfiguration.locale = Locale.US;
            }

            mResources.updateConfiguration(mConfiguration, mDisplayMetrics);

        } else if (mCity_new.equals("2")) {

            //17以上利用 setLocale（）
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                mConfiguration.setLocale(Locale.JAPANESE);
            } else {
                mConfiguration.locale = Locale.JAPAN;
            }
            mResources.updateConfiguration(mConfiguration, mDisplayMetrics);


        } else {

            Locale spanish = new Locale("es", "rES");

            //17以上利用 setLocale（）
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                mConfiguration.setLocale(spanish);
            } else {
                mConfiguration.locale = spanish;
            }
            mResources.updateConfiguration(mConfiguration, mDisplayMetrics);
        }
    }


    private void judge(Editable editable) {
        String temp = editable.toString();
        int posDot = temp.indexOf(".");
        //直接输入小数点的情况
        if (posDot == 0) {
            editable.insert(0, "0");
            return;
        }
        //连续输入0
        if (temp.equals("00")) {
            editable.delete(1, 2);
            return;
        }
        //输入"08" 等类似情况
        if (temp.startsWith("0") && temp.length() > 1 && (posDot == -1 || posDot > 1)) {
            editable.delete(0, 1);
            return;
        }

        //不包含小数点 不限制小数点前位数

        if (posDot < 0 && beforeDot == -1) {
            //do nothing 仅仅为了理解逻辑而已
            return;
        } else if (posDot < 0 && beforeDot != -1) {
            //不包含小数点 限制小数点前位数
            if (temp.length() <= beforeDot) {
                //do nothing 仅仅为了理解逻辑而已
            } else {
                editable.delete(beforeDot, beforeDot + 1);
            }
            return;
        }

        //如果包含小数点 限制小数点后位数
        if (temp.length() - posDot - 1 > afterDot && afterDot != -1) {
            editable.delete(posDot + afterDot + 1, posDot + afterDot + 2);//删除小数点后多余位数
        }
    }


}
